package com.hancher.plugins.hrsearchview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class HrSearchView extends RelativeLayout {
    private EditText et_search;
    private View img_clear;
    private View img_left_icon;
    private View img_scan;
    HrOnOperationListener listener;
    private static final String TAG = "HrSearchView";

    public HrSearchView(Context context) {
        super(context);
        init(context, null);
    }

    public HrSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HrSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public HrSearchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    int layout = R.layout.hr_search_view;
    String hint = "";
    boolean hideLeftIcon = false;
    boolean hideRightScan = false;

    private void init(Context context, AttributeSet attrs) {
        Log.d(TAG, "init: ");
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HrSearchView);
            layout = typedArray.getResourceId(R.styleable.HrSearchView_hr_layout, R.layout.hr_search_view);
            hint = typedArray.getString(R.styleable.HrSearchView_hr_hint);
            hideLeftIcon = typedArray.getBoolean(R.styleable.HrSearchView_hr_hide_left_icon, false);
            hideRightScan = typedArray.getBoolean(R.styleable.HrSearchView_hr_hide_right_scan, false);
            Log.d(TAG, "init: hint=" + hint);
        }
        View contentView = inflate(context, layout, this);
        et_search = contentView.findViewById(R.id.et_search);
        if (!TextUtils.isEmpty(hint)) {
            et_search.setHint(hint);
        }
        img_clear = contentView.findViewById(R.id.img_clear);
        img_left_icon = contentView.findViewById(R.id.img_left_icon);
        if (hideLeftIcon) {
            img_left_icon.setVisibility(View.GONE);
        }
        img_scan = contentView.findViewById(R.id.img_scan);
        if (hideRightScan) {
            img_scan.setVisibility(View.GONE);
        }

        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = editable.toString();
                boolean isEmpty = text.isEmpty();
                img_clear.setVisibility(isEmpty ? View.GONE : View.VISIBLE);
                if (listener != null) {
                    listener.onTextChanged(text);
                    if (isEmpty) {
                        listener.onClearClick();
                    }
                }
            }
        });
        img_clear.setOnClickListener(view -> et_search.setText(""));

        et_search.setOnEditorActionListener((v, actionId, event) -> {
            String code = et_search.getText().toString();
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (listener != null) {
                    listener.onSearchEnter(et_search, code);
                }
            }
            return false;
        });
        img_scan.setOnClickListener(view -> {
            if (listener != null) {
                listener.onScanClick();
            }
        });
    }

    public HrOnOperationListener getListener() {
        return listener;
    }

    public void setListener(HrOnOperationListener listener) {
        this.listener = listener;
    }

    public Editable getText() {
        return et_search.getText();
    }

    public void setText(CharSequence text) {
        et_search.setText(text);
    }
}
